<template>
  <a-form :rules="jobRules" :model="jobFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="jobFormRef" :disabled="hasPerm('monitor:job:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="定时任务调度">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="jobTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="任务名称" name="jobName">
              <a-input allow-clear placeholder="请输入任务名称" v-model:value="jobFormConfig.form.jobName">
              </a-input>
            </a-form-item>
            <a-form-item label="任务组名" name="jobGroup">
              <a-input allow-clear placeholder="请输入任务组名" v-model:value="jobFormConfig.form.jobGroup">
              </a-input>
            </a-form-item>
            <a-form-item label="调用目标字符串" name="invokeTarget">
              <a-textarea allow-clear placeholder="请输入调用目标字符串"
                v-model:value="jobFormConfig.form.invokeTarget"></a-textarea>
            </a-form-item>
            <a-form-item label="cron执行表达式" name="cronExpression">
              <a-input allow-clear placeholder="请输入cron执行表达式" v-model:value="jobFormConfig.form.cronExpression">
              </a-input>
            </a-form-item>
            <a-form-item label="计划执行错误策略" name="misfirePolicy">
              <a-input allow-clear placeholder="请输入计划执行错误策略（1立即执行 2执行一次 3放弃执行）"
                v-model:value="jobFormConfig.form.misfirePolicy">
              </a-input>
            </a-form-item>
            <a-form-item label="是否并发执行" name="concurrent">
              <a-input allow-clear placeholder="请输入是否并发执行（0允许 1禁止）" v-model:value="jobFormConfig.form.concurrent">
              </a-input>
            </a-form-item>
            <a-form-item label="状态" name="status">
              <a-input allow-clear placeholder="请输入状态（0正常 1暂停）" v-model:value="jobFormConfig.form.status">
              </a-input>
            </a-form-item>
            <a-form-item label="更新者" name="updateBy">
              <a-input allow-clear placeholder="请输入更新者" v-model:value="jobFormConfig.form.updateBy">
              </a-input>
            </a-form-item>
            <a-form-item label="更新时间" name="updateTime">
              <FormDate v-model="jobFormConfig.form.updateTime">
              </FormDate>
            </a-form-item>
            <a-form-item label="备注信息" name="remark">
              <a-textarea allow-clear placeholder="请输入备注信息" v-model:value="jobFormConfig.form.remark"></a-textarea>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createJob,
  updateJob,
} from '@/api/modules/monitor/job/job';
import { notify, } from '@/utils/table/table';
import { jobList } from '../data/curd';
import {
  jobFormConfig,
  jobRules,
  jobFormRef
} from '../data/form';
import { jobTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  jobTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (jobFormConfig.value.form.jobId) {
    const { data } = await updateJob(jobFormConfig.value.form)
    result = data
  } else {
    const { data } = await createJob(jobFormConfig.value.form)
    result = data
  }
  notify(result)
  jobTable.value.loading = false;
  await jobList();
  back();

};

const back = () => {
  jobFormConfig.value.display = false;
  jobFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
